#ifndef MUSIC_TOOL_H
#define MUSIC_TOOL_H

#include <iostream>

class MusicTool
{
public:
	virtual void play() = 0;
	//virtual MusicTool* clone() = 0;
};

class BasicMusicTool : public MusicTool
{
public:
	BasicMusicTool()
	{
		len = 0;
		pName = NULL;
	}

	BasicMusicTool(const BasicMusicTool& tool)
	{
		if (this != &tool)
		{
			copy(tool.len, tool.pName);
		}
	}

	~BasicMusicTool()
	{
		if (!pName)
		{
			delete pName, pName = NULL;
		}
	}

public:
	virtual void play()
	{
		std::cout<<pName<<" is playing..."<<std::endl;
	}

	//virtual BasicMusicTool* clone() = 0;

	void copy(int iLen, char *pStr)
	{
		if (pName == pStr)
			return;

		if (!pName)
		{
			delete pName, pName = NULL;
		}

		len = iLen;
		pName = new char[len + 1];
		memcpy(pName, pStr, len);
		*(pName + len) = 0;
	}

protected:
	int len;
	char *pName;
};

class Piano : public BasicMusicTool
{
public:
	Piano()
	{
		copy(5, "Piano");
	}

public:
	Piano* clone()
	{
		return new Piano(*this);
	}
};

class Flute : public BasicMusicTool
{
public:
	Flute()
	{
		copy(5, "Flute");
	}

public:
	Flute* clone()
	{
		return new Flute(*this);
	}
};

#endif // MUSIC_TOOL_H
